package com.daxt.controller.accept;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.common.result.*;
import com.daxt.mapper.accept.WorkOrderMapper;
import com.daxt.mapper.archives.ArchivesSourceFondsMapper;
import com.daxt.model.service.accept.vo.WorkOrder;
import com.daxt.model.service.archives.vo.ArchivesSourceFonds;
import com.daxt.model.service.base.result.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "归档任务单")
@RestController
public class WorkOrderController {

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private ArchivesSourceFondsMapper archivesSourceFondsMapper;

    @ApiOperation("根据用户权限获取对应的系统来源")
    @GetMapping("/workOrder/source")
    @LoginAuth()
    public Result<List<ArchivesSourceFonds>> archivesSourceFonds( @ApiIgnore @LoginUser UserInfo loginUser){
        QueryWrapper<ArchivesSourceFonds> sourceFondsQueryWrapper=new QueryWrapper<>();
        if("admin".equals(loginUser.getUsername()) || "超级管理员".equals(loginUser.getRole()) ){

        }else{
            sourceFondsQueryWrapper.eq("fondsId",loginUser.getFondsId());
        }
        List<ArchivesSourceFonds> data = archivesSourceFondsMapper.selectList(sourceFondsQueryWrapper);
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
    }

    @ApiOperation("获取归档任务单列表")
    @GetMapping("/workOrder")
    @LoginAuth()
    public Result<PageInfo<WorkOrder>> listLog(Integer pageNum, Integer pageSize,
                                               @ApiParam("接口名称：如OA") @RequestParam(value = "appId",required = false) String appId,
                                               @ApiParam("状态 0代表失败，1代表成功") @RequestParam(value = "state",required = false) String state ,
                                               @ApiParam("检索关键词") @RequestParam(value = "keyWord",required = false) String keyWord,
                                               @ApiParam("开始时间") @RequestParam(value = "startTime",required = false) String startTime,
                                               @ApiParam("结束时间") @RequestParam(value = "endTime",required = false) String endTime,
                                               @ApiIgnore @LoginUser UserInfo loginUser){

        Page<WorkOrder> page = new Page<>(pageNum == null ? 0 : pageNum,
                pageSize == null ? 10 : pageSize);

        QueryWrapper<WorkOrder> queryWrapper=new QueryWrapper<>();

        if("admin".equals(loginUser.getUsername()) || "超级管理员".equals(loginUser.getRole()) ){

        }else{
            QueryWrapper<ArchivesSourceFonds> sourceFondsQueryWrapper=new QueryWrapper<>();
            sourceFondsQueryWrapper.eq("fondsId",loginUser.getFondsId());
            List<ArchivesSourceFonds> archivesSourceFondsList = archivesSourceFondsMapper.selectList(sourceFondsQueryWrapper);
            List<String> appIdList=new ArrayList<>();
            for(ArchivesSourceFonds archivesSourceFonds:archivesSourceFondsList){
                appIdList.add(archivesSourceFonds.getSourceName());
            }
            if(!appIdList.isEmpty()) queryWrapper.in("appId",appIdList);
        }

        if(!StringUtils.isEmpty(appId)) queryWrapper.eq("appId",appId);
        if(!StringUtils.isEmpty(state)) queryWrapper.eq("state",state);
        if(!StringUtils.isEmpty(startTime)) queryWrapper.ge("gdsj",startTime);
        if(!StringUtils.isEmpty(endTime)) queryWrapper.le("gdsj",endTime);
        if(!StringUtils.isEmpty(keyWord)){
            queryWrapper.and( workOrderQueryWrapper -> workOrderQueryWrapper.like("companyCode",keyWord).or()
                                                        .like("ywjc",keyWord).or()
                                                        .like("gdsm",keyWord).or()
                                                        .like("rwdbt",keyWord).or()
                                                        .like("reason",keyWord));
        }
        queryWrapper.orderByDesc("createData");

        IPage<WorkOrder> info =workOrderMapper.selectPage(page,queryWrapper);
        PageInfo<WorkOrder> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
                info.getRecords());
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
    }
}
